package com.scada.model.result;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.springframework.web.servlet.ModelAndView;

public class ResponseBean implements Serializable{
	
	private static final long serialVersionUID = 4836516488186911004L;
	//返回的语句
	private String msg;
	//1 失败 2 成功
	private String type;
	
	public final static String SUCCESS = "2";
	
	public final static String FAILED = "1";
	
	private Map<String, Object> resultMap = new HashMap<String, Object>();
	
	public ResponseBean(){
		failed();
	}

	public String getMsg() {
		return msg;
	}

	@SuppressWarnings("unused")
	private void setMsg(String msg) {
		this.msg = msg;
	}

	public String getType() {
		return type;
	}

	@SuppressWarnings("unused")
	private void setType(String type) {
		this.type = type;
	}

	public Map<String, Object> getResultMap() {
		return resultMap;
	}

	@SuppressWarnings("unused")
	private void setResultMap(Map<String, Object> resultMap) {
		this.resultMap = resultMap;
	}
	
	public void addObject(String key, Object value){
		resultMap.put(key, value);
	}
	
	public void toMessage(String message){
		msg = message;
	}
	
	@SuppressWarnings("static-access")
	public void success(){
		type = this.SUCCESS;
		msg = "成功";
	}
	
	@SuppressWarnings("static-access")
	public void success(String message){
		type = this.SUCCESS;
		msg = message;
	}
	
	@SuppressWarnings("static-access")
	public void failed(){
		type = this.FAILED;
		msg = "失败";
	}
	
	@SuppressWarnings("static-access")
	public void failed(String message){
		type = this.FAILED;
		msg = message;
	}
	
	public Object getObject(String key) {
		return resultMap.get(key);
	}
	
	public ModelAndView getJsonModelView() {
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("type", type);
		model.put("msg", msg);

		for (String key : resultMap.keySet()) {
			Object value = resultMap.get(key);
			model.put(key, value);
		}
		return new ModelAndView("jsonView", model);
	}
	
	public Map<String, Object> getModelMap(){
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("type", type);
		model.put("msg", msg);

		for (String key : resultMap.keySet()) {
			Object value = resultMap.get(key);
			model.put(key, value);
		}
		
		return model;
	}
}
